/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Structures;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.LinkedList;

/**
 *
 * @author Krit
 */
public class ProfileDataBase implements Serializable {
    public LinkedList<ReceiptProfile> profiles;

    public ProfileDataBase() {
        profiles = null;
        try {
            FileInputStream in = new FileInputStream("profiles.bin");
            ObjectInputStream ois = new ObjectInputStream(in);
            profiles = (LinkedList<ReceiptProfile>)ois.readObject();
        } catch (Exception e) {
            profiles = null;
            System.out.println("Problem serializing: " + e);
        }
        if(profiles == null)
            profiles = new LinkedList<ReceiptProfile>();
        
        if(profiles.size() < 3)
            try {
                profiles.add(new AuchanProfile());
                profiles.add(new BiedronkaProfile());
                profiles.add(new CarrefourProfile());
            } catch (Exception e) {
                System.out.println("Problem with adding default profiles: " + e);
            }
    }
    
    public boolean saveProfilesOnHDD(){
        try {
            File file = new File ("profiles.bin");
            if ( file.exists() )
                file.delete();
            
            file.createNewFile();
            FileOutputStream out = new FileOutputStream("profiles.bin");
            ObjectOutputStream oos = new ObjectOutputStream(out);
            oos.writeObject(profiles);
            oos.flush();
        } catch (Exception e) {
            System.out.println("Problem serializing: " + e);
            return false;
        }
        return true;
    }

    public String[] getProfilesArray(){
        String[] lines = new String[profiles.size()];

        int i = 0;
        for(ReceiptProfile p: profiles){
            lines[i++] = p.getName();
        }

        return lines;
    }

}
